package com.ysynzehao.service.impl;

import com.ysynzehao.bean.User;
import com.ysynzehao.dao.UserRepository;
import com.ysynzehao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * niuzehao
 * 2018/11/30 18:06.
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public Page<User> getAllUser(Pageable pageable) {
        return userRepository.findAll(pageable);
    }

    @Override
    public Page<User> getAllUser2(Pageable pageable, User user) {
        Specification<User> specifications = new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicate = new ArrayList<>();
                if (!StringUtils.isEmpty(user.getUserName())) {
                    Expression<String> path = root.get("userName");
                    predicate.add(cb.like(path, "%" + user.getUserName() + "%"));
                }
                Predicate[] pre = new Predicate[predicate.size()];
                return query.where(predicate.toArray(pre)).getRestriction();
            }
        };
        return userRepository.findAll(specifications, pageable);
    }

    @Override
    public List<Map> getUserMap() {
        User user = new User();
        user.setUserName("11510");
        List<Map> userMapList = userRepository.findAllByParams("select * from user where user_name = #{userName}", user);
        return userMapList;
    }

    @Override
    public List<User> findByAgeIsLessThanAndUserNameLike(int age, String userName) {
        return userRepository.findByAgeIsLessThanAndUserNameLike(age, userName);
    }

    @Override
    public List<User> findByUserNameHql(String userName) {
        return userRepository.findByUserNameHql(userName);
    }

    @Override
    public List<Map<Integer, String>> findUserIdNameMap() {
        return userRepository.findUserIdNameMap();
    }

    @Override
    public List<User> findByUserNameAndAgeSql(String userName, int age) {
        return userRepository.findByUserNameAndAgeSql(userName, age);
    }

    @Override
    public User saveUser(User user) {
        return userRepository.save(user);
    }

    /**
     * unless = "#result == null" 如果返回值为null,不缓存结果
     * @param userId
     * @return
     */
    @Override
    public User getUser(int userId) {
        Optional<User> user = userRepository.findById(userId);
        return user.get();
    }

    @Override
    public User updateUser(User user) {
        return userRepository.save(user);
    }

    @Override
    public void deleteUser(int userId) {
        userRepository.deleteById(userId);
    }
}
